//
//  LGLoginViewController.swift
//  aj
//
//  Created by leeqb on 11/18/15.
//  Copyright © 2015 powerlee. All rights reserved.
//

import UIKit

class LGLoginViewController: AJBaseTableViewController, UITextFieldDelegate {
    @IBOutlet weak var bannerView: UIView!
    @IBOutlet weak var number: UITextField!
    @IBOutlet weak var numberBgView: UIImageView!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var passwordBgView: UIImageView!
    @IBOutlet weak var loginButton: CBButton!
    
    //MARK: - Overrides
    override func viewDidLoad() {
        super.viewDidLoad()
        AJBaseController.addWhiteStatusBar(self)
        
        bannerView.backgroundColor = UIColor(patternImage: UIImage(named: "lg_banner_bg")!)
        self.tableView.zeroInsets()
        self.tableView.delaysContentTouches = false
        
        //self.tableView.separatorColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.05)
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBarHidden = true
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBarHidden = false
    }
    
    //MARK: - Public Methods
    class func showLoginView() {
        let loginNav = LBI("LGLogin") as! UINavigationController
        let rootVC = UIApplication.sharedApplication().keyWindow?.rootViewController
        rootVC?.presentViewController(loginNav, animated: true, completion: { () -> Void in
        })
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if textField.tag == 1 {
            password.becomeFirstResponder()
        } else if textField.tag == 2 {
            self.doLogin(self.loginButton)
        } else {
            return true
        }
        
        return false
    }
    
    //MARK: - Button Clicks
    @IBAction func Resign() {
        let serveVC = LB("LGLogin", vcName: "ServeClause") as! UIViewController
        self.navigationController?.pushViewController(serveVC, animated: true)
    }
    
    @IBAction func forgetPwd() {
        let user = number.text!
        if user.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入手机号")
            return
        }
        
        let forgetVC = LB("LGLogin", vcName: "RegisterTwo") as! LGRegisterTwoViewController
        forgetVC.navigationItem.title = "忘记密码"
        forgetVC.maskNum = user
        self.navigationController?.pushViewController(forgetVC, animated: true)
    }
    
    @IBAction func doLogin(sender: CBButton) {
        let user = number.text!
        let pass = password.text!
        
        if user.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入爱家号或手机号")
            return
        }
        
        if pass.isEmpty {
            CBAlertView.shareInstance().showMsg("请输入密码")
            return
        }
        
        number.resignFirstResponder()
        password.resignFirstResponder()
        
        let params = ["userTel": user,"userPwd": pass]
        CBMessage.shareInstance().showProcessing()
        AJNetworkHelper.instance.post("aj_user_login", parameters: params, success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            // 请求成功
            if (responseObject["returnCode"] as? String == RetCodeSuccess){
                let result = responseObject["result"] as! [NSObject : AnyObject]
                DataManager.setUserInfo(result)
                UIApplication.sharedApplication().keyWindow?.rootViewController = TabBarController()
                /*let userId = result["userId"] as? String ?? ""
                DataManager.refreshUserInfo(userId, success: { () -> Void in
                    UIApplication.sharedApplication().keyWindow?.rootViewController = TabBarController()
                    }, failed: { () -> Void in
                        CBAlertView.shareInstance().showMsg("登录失败")
                })*/
            } else {
                CBAlertView.shareInstance().showMsg(responseObject["errorMsg"] as? String)
            }
            }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                CBAlertView.shareInstance().showMsg("登录失败")
            }) { () -> Void in
                CBMessage.shareInstance().dismiss()
        }
    }
    
}
